import axios from "axios";
import qs from "qs";
import {Toast} from 'antd-mobile';

let baseUrl = "";
let imgPre = '';
//配置环境
if (process.env.NODE_ENV === "development") {
    //开发环境
    baseUrl = "/api";
    imgPre = "http://localhost:2000";
} else {
    //生产环境
}

export {imgPre};

//请求拦截
axios.interceptors.request.use(config => {
    //请求拦截逻辑
    // console.log(config);
    config.url = baseUrl + config.url;
    return config;
})

//响应拦截
axios.interceptors.response.use(res => {
    //响应拦截逻辑
    console.group("本次请求地址：" + res.config.url)
    console.log(res);
    console.groupEnd()

    if (res.data.code !== 200) {
        Toast.show({
            content: res.data.msg
        })
    }

    return res;
})


export const get = (url, params = {}) => {
    return axios({
        url,
        params
    })
}

export const post = (url, params = {}, haveFile = false) => {
    let data = null;
    if (haveFile) {
        data = new FormData();
        for (let i in params) {
            data.append(i, params[i]);
        }
    } else {
        data = qs.stringify(params);
    }

    return axios({
        url,
        method: 'post',
        data
    })
}